package com.xiaoye.qbproject.controller.base;

import com.xiaoye.qbproject.domain.AjaxResult;
import com.xiaoye.qbproject.domain.QbUser;
import com.xiaoye.qbproject.service.QbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : ChenYao
 * @version : 1.0
 * @description :
 * @create : 2024-12-26 22:29
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private QbUserService userService;

    @GetMapping()
    public AjaxResult list() {
        List<QbUser> list = userService.list();
        list.stream().forEach(qbUser -> {
            qbUser.setPassword("******");
        });
        return AjaxResult.success(list);
    }

    @PostMapping("/add")
    public AjaxResult addUser(@RequestBody QbUser user) {
        String username = user.getUsername();
        if (username == null || username.isEmpty()) {
            return AjaxResult.error("用户名不能为空");
        }
        String password = user.getPassword();
        if (password == null || password.isEmpty()) {
            return AjaxResult.error("密码不能为空");
        }
        boolean save = userService.save(user);
        if (!save) {
            return AjaxResult.error("添加失败");
        }
        return AjaxResult.success("添加成功");
    }
}
